﻿using System;
using Microsoft.Practices.ObjectBuilder2;

namespace TSharp.Core.Asm
{
    /// <summary>
    /// 扩展点标记
    /// <para>2011/3/4</para>
    /// 	<para>TANGJINGBO</para>
    /// 	<author>tangjingbo</author>
    /// </summary>
    [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
    public sealed class ExtensionPointAttribute : Attribute
    {
        internal static readonly Type ExtensionAttributeType = typeof(ExtensionAttribute);
        internal static readonly Type ExtensionPointType = typeof(ExtensionPoint);

        /// <summary>
        /// 定义扩展点
        /// </summary>
        /// <param name="attributeType">扩展标记类型</param>
        /// <param name="pointType">扩展点收集类的类型</param>
        public ExtensionPointAttribute(Type attributeType, Type pointType)
        {
            if (!ExtensionAttributeType.IsAssignableFrom(attributeType))
            {
                throw new ExtensionException(attributeType.Name + " not extend ExtensionAttribute");
            }
            if (!ExtensionPointType.IsAssignableFrom(pointType))
            {
                throw new ExtensionException(pointType.Name + " not extend IExtensionPoint");
            }
            AttributeType = attributeType;
            PointType = pointType;
        }

        /// <summary>
        /// 扩展标记类型
        /// </summary>
        public Type AttributeType { get; private set; }

        /// <summary>
        /// 扩展点收集类的类型
        /// </summary>
        public Type PointType { get; private set; }

        /// <summary>
        /// 扩展点收集类实例
        /// </summary>
        internal ExtensionPoint ExtensionPoint
        {
            get
            {
                return (ExtensionPoint)PointType.GetConstructor(new Type[0]).Invoke(new object[0]);
            }
        }
    }
}